import os

from django.shortcuts import render, redirect
from django.http import HttpResponse
from OSCodeApp import models
import json


def admin(request):
    if request.method == 'GET':
        return render(request, 'adminlogin.html')
    elif request.method == 'POST':
        admin_id = request.POST.get('admin_id')
        admin_pwd = request.POST.get('admin_pwd')
        response = {'msg': '', 'status': False}
        if models.Admin.objects.filter(admin_id=admin_id, admin_pwd=admin_pwd):
            response['status'] = True
            request.session['uid'] = admin_id
            return HttpResponse(json.dumps(response))
            pass
        else:
            response['msg'] = '用户名或密码错误'
            return HttpResponse(json.dumps(response))
            pass


# 公告管理
def announcement_manage(request):
    if not request.session.get('admin_id'):
        return redirect('/adminlogin')
    if request.method == 'GET':
        announcements = models.Announcement.objects.filter()
        response = {'announcements': announcements}
        return render(request, 'announcementmanage.html', response)
    elif request.method == 'POST':
        p_type = request.POST.get('type')
        response = {'msg': '', 'status': False}
        if p_type == 'delete':
            a_id = request.POST.get('a_id')
            models.Announcement.objects.filter(id=a_id).delete()
            response['msg'] = '已删除该公告'
            response['status'] = True
        elif p_type == 'create':
            a_title = request.POST.get('a_title')
            a_content = request.POST.get('a_content')
            models.Announcement.objects.create(a_title=a_title, a_content=a_content)
            response['msg'] = '已新建公告'
            response['status'] = True
        return HttpResponse(json.dumps(response))


# 项目管理
def project_manage(request):
    if not request.session.get('admin_id'):
        return redirect('/adminlogin')
    if request.method == 'GET':
        projects = models.Project.objects.filter()
        response = {
            'projects': projects,
        }
        return render(request, 'projectmanage.html', response)
    elif request.method == 'POST':
        p_type = request.POST.get('type')
        response = {'msg': '', 'status': False}
        print(p_type)

        if p_type == 'delete':
            pid = request.POST.get('pid')
            projects = models.Project.objects.filter(id=pid)
            for project in projects:
                project.delete()
                os.remove(str(project.p_prepic).split('/', 1)[1])
                Comments = models.Comment.objects.filter(c_pid=project.id)
                for comment in Comments:
                    comment.delete()
                    os.remove(str(comment.c_pic).split('/', 1)[1])
            response['msg'] = '已删除该项目'
            response['status'] = True
        if p_type == 'recommend':
            print('精选')
            pid = request.POST.get('pid')
            models.Project.objects.filter(id=pid).update(p_isrecommended=True)
            response['msg'] = '已推荐该项目'
            response['status'] = True
        if p_type == 'cancelrecommend':
            pid = request.POST.get('pid')
            models.Project.objects.filter(id=pid).update(p_isrecommended=False)
            response['msg'] = '已取消对该项目的推荐'
            response['status'] = True
        return HttpResponse(json.dumps(response))


# 类别管理
def category_manage(request):
    if not request.session.get('admin_id'):
        return redirect('/adminlogin')
    if request.method == 'GET':
        categories = models.Category.objects.filter()
        response = {
            'categories': categories
        }
        return render(request, 'categorymanage.html', response)
    if request.method == 'POST':
        p_type = request.POST.get('type')
        response = {'msg': '', 'status': False}
        if p_type == 'delete':
            c_id = request.POST.get('c_id')

            models.Category.objects.filter(id=c_id).delete()
            projects = models.Project.objects.filter(p_category=c_id)
            for project in projects:
                project.delete()
                os.remove(str(project.p_prepic).split('/', 1)[1])
            response['msg'] = '已删除该类别'
            response['status'] = True
        if p_type == 'create':
            c_name = request.POST.get('c_name')
            models.Category.objects.create(c_name=c_name)
            response['msg'] = '已新建类别'
            response['status'] = True
        return HttpResponse(json.dumps(response))
